#ifndef BLE_FUNC_H
#define BLE_FUNC_H 
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include "SimpleMap.h"
#include "queue.h"

class BTDevice
{
public :
    BTDevice(){
      
    }
    String deviceId;
    String address;
    String name;
    String nickName;
};

class RemoteBle
{
private:
  // The remote service we wish to connect to.
  BLEUUID serviceUUID;
  // The characteristic of the remote service we are interested in.
  BLEUUID readUUID;
  BLEUUID writeUUID;
  BLEAdvertisedDevice *myDevice; // 远程蓝牙对象信息
  BLEClient *pClient;
  BLERemoteCharacteristic *readCharacter;
  BLERemoteCharacteristic *writeCharacter;
  BTDevice* btDevice;
  Queue queue;

public:


 BTDevice* getBTDevice(){
   
   return btDevice;
 }


  ~RemoteBle()
  {
    if(pClient!=nullptr){
       pClient->disconnect();
       delete pClient;
    }
    if(myDevice!=nullptr){
      delete myDevice;
    }
    if(readCharacter!=nullptr){
      delete readCharacter;
    }
    if(writeCharacter!=nullptr){
      delete writeCharacter; 
    }
  }



  RemoteBle()
  {
  }

  RemoteBle(BTDevice* device)
  {
    this->btDevice = device;
  }

    void setAdvertisedDevice(BLEAdvertisedDevice& device){
        myDevice = new BLEAdvertisedDevice(device);
    }

  String getName(){
    if(myDevice==nullptr||myDevice->getName().empty()){
      return String("");
    }

    return  String(myDevice->getName().c_str());
  }
  
  RemoteBle(BLEAdvertisedDevice device)
  {
    myDevice = new BLEAdvertisedDevice(device);
  }

  bool connect();

  bool isConnect();

  

  String getRemoteAddress();
 
  bool readValue(String &s);

  bool writeValue(String value);
  
  // 重载==运算符
  bool operator==(const RemoteBle &other) const;
};

void sendNotify(String msg);
void loopBle();
void setupBle();
void startAdvertising();
void stopAdvertising();
bool isAdvertising();
void setAdvertising(bool b);
void setNeedStartAdvertising(bool);
bool isNeedStartAdvertising();
String parseMacAddress(String data);
void setupScan();
void startScan();
bool connectToServer();
void readAllBleData();





#endif